//1.导入路由组件
import { createRouter, Router, createWebHistory } from 'vue-router';


//2.定义路由组件
const routes = [
    {
        path: '/',
        redirect: '/login',
        component: () => import('@/views/LoginView.vue')
    },
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/LoginView.vue')
    },
    {
        path: '/main',
        name: 'main',
        component: () => import('@/views/MainView.vue'),
        redirect: '/home',
        children: [
            {
                path: '/user',
                name: 'user',
                component: () => import('@/views/UserView.vue')
            },
            {
                path: '/home',
                name: 'home',
                component: () => import('@/views/HomeView.vue')
            },
            {
                path: '/secondkill',
                name: 'secondkill',
                component: () => import('@/views/SecondKillView.vue')
            },
            {
                path: '/land',
                name: 'land',
                component: () => import('@/views/LandView.vue')
            },
            {
                path: '/file',
                name: 'file',
                component: () => import('@/views/FileUpload.vue')
            }


        ]
    }
];

//3.创建路由实例

const router = createRouter({
    history: createWebHistory(),
    routes
});



//4.路由守卫   from：从哪里来  to：到哪里去 next：下一步跳转操作
router.beforeEach((to, from, next) => {
    /*
    1.获取url
    2.白名单：放过
    3.需要校验的
        A.是否有token
        B.是否过期
        C.权限
    */

    //1.获取url
    const whiteList = ['/login', '/register'];
    console.log(to, from, next);
    for (let white of whiteList) {
        if (to.path === white) {
            return next();
        }
    }
    let token = sessionStorage.getItem('token');
    if (!token) {
        next('/login');
    } else {
        next();
    }


})


//5.导出路由实例
export default router;